package erz.printing.police.notch;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

class HwNotchScreenSupport implements INotchScreenSupport {
    //华为刘海屏全屏显示FLAG
    private final int HW_FLAG_NOTCH_SUPPORT = 0x00010000;

    private Class<?> hwNotchSizeUtil = null;
    private Field hwLayoutParamsFlags = null;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public boolean hasNotchInScreen(Window window) {
         try {
            if (hwNotchSizeUtil == null) {
                ClassLoader cl = window.getContext().getClassLoader();
                hwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            }
            if (hwNotchSizeUtil == null) return false;
            Method get = hwNotchSizeUtil.getMethod("hasNotchInScreen");
            return (boolean) get.invoke(null);
        } catch (Exception e) {
            return  false;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public List<Rect> getNotchSize(Window window) {
        ArrayList<Rect> result = new ArrayList<>();
        Rect rect = new Rect();
        try {
            Context context = window.getContext();
            if (hwNotchSizeUtil == null) {
                ClassLoader cl = context.getClassLoader();
                hwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            }
            if (hwNotchSizeUtil == null) return result;

            Method get = hwNotchSizeUtil.getMethod("getNotchSize");
            int[] ret = (int[])get.invoke(null);
            rect.left = (context.getResources().getDisplayMetrics().widthPixels - ret[0]) / 2;
            rect.bottom = ret[1];
            rect.right = rect.left + ret[0];
            rect.top = 0;
            result.add(rect);
            return result;
        } catch (Exception e) {
            return result;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void setWindowLayoutAroundNotch(Window window) {
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        try {
            if (hwLayoutParamsFlags == null) {
                hwLayoutParamsFlags = layoutParams.getClass().getDeclaredField("hwFlags");
                hwLayoutParamsFlags.setAccessible(true);
            }
            int old = (int)hwLayoutParamsFlags.get(layoutParams);
            hwLayoutParamsFlags.set(layoutParams, old | HW_FLAG_NOTCH_SUPPORT);
        } catch (Exception e) {
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void setWindowLayoutBlockNotch(Window window) {
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        try {
            if (hwLayoutParamsFlags == null) {
                hwLayoutParamsFlags = layoutParams.getClass().getDeclaredField("hwFlags");
                hwLayoutParamsFlags.setAccessible(true);
            }
            int old = (int)hwLayoutParamsFlags.get(layoutParams);
            hwLayoutParamsFlags.set(layoutParams, old & (~HW_FLAG_NOTCH_SUPPORT));
        } catch (Exception e) {
        }
    }
}
